package com.zcoj.Scheduled;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcoj.annotation.aspect.ContestInProgressAspect;
import com.zcoj.mapper.CommentMapper;
import com.zcoj.mapper.DiscussionLikeMapper;
import com.zcoj.mapper.DiscussionMapper;
import com.zcoj.mapper.ReplyMapper;
import com.zcoj.pojo.Dto.Comment;
import com.zcoj.pojo.Dto.Discussion;
import com.zcoj.pojo.Dto.DiscussionLike;
import com.zcoj.pojo.Dto.Reply;
import com.zcoj.service.ContestService;
import com.zcoj.service.RankService;
import com.zcoj.util.RedisCaches;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class MyScheduledTask {

    private final TaskExecutor taskExecutor;

    @Autowired
    private RedisCaches redisCaches;

    @Autowired
    private DiscussionMapper discussionMapper;


    @Autowired
    private DiscussionLikeMapper discussionLikeMapper;


    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private RankService rankService;

    @Autowired
    private ContestService contestService;

    @Autowired
    private ContestInProgressAspect contestInProgressAspect;



    public MyScheduledTask(@Qualifier("taskExecutor") TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

    //@Scheduled(fixedRate = 5000)
    public void runTask() {
        taskExecutor.execute(() -> {
            log.info("定时任务执行，线程名：{}" ,Thread.currentThread().getName());
        });
    }


    /**
     * @method: [discussionClickLike]
     * @param: []
     * @returns: void
     * @description: 论坛版块点赞 更新
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/4/7 08:16
     **/
    @Scheduled(cron = "0 0 */2 * * ?")
    public void discussionClickLike() {
        taskExecutor.execute(() -> {
            log.info("discussionClickLike定时任务执行，线程名：{}" ,Thread.currentThread().getName());
            QueryWrapper<Discussion> queryWrapper=new QueryWrapper<>();
            queryWrapper.select("id");
            for (Discussion discussion : discussionMapper.selectList(queryWrapper)) {
              QueryWrapper<DiscussionLike> discussionLikeQueryWrapper=new QueryWrapper<>();
              discussionLikeQueryWrapper.eq("did",discussion.getId());
                Integer integer = discussionLikeMapper.selectCount(discussionLikeQueryWrapper);
                if(Objects.equals(discussion.getLikeNum(),integer)){
                    continue;
                }else {
                    discussion.setLikeNum(integer);
                    discussionMapper.updateById(discussion);
                }
            }

        });
    }


    /**
     * @method: [discussionCommentNum]
     * @param: []
     * @returns: void
     * @description:     评论数量统计
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/4/10 09:29
     **/
    @Scheduled(cron = "0 0 */2 * * ?")
    public void discussionCommentNum() {
        taskExecutor.execute(() -> {
            log.info("discussionCommentNum，线程名：{}" ,Thread.currentThread().getName());
            QueryWrapper<Discussion> queryWrapper=new QueryWrapper<>();
            queryWrapper.select("id");
            for (Discussion discussion : discussionMapper.selectList(queryWrapper)) {
                QueryWrapper<Comment> commentQueryWrapper=new QueryWrapper<>();
                commentQueryWrapper.eq("did",discussion.getId());
                Integer commentNum = commentMapper.selectCount(commentQueryWrapper);
                QueryWrapper<Reply> replyQueryWrapper=new QueryWrapper<>();
                replyQueryWrapper.eq("did",discussion.getId());
                Integer replyNum = replyMapper.selectCount(replyQueryWrapper);
                if(Objects.equals(discussion.getCommentNum(),commentNum+replyNum)){
                    continue;
                }else {
                    discussion.setCommentNum(commentNum+replyNum);
                }

            }

        });
    }

    /**
     * @method: [calculateOiRank]
     * @param: []
     * @returns: void
     * @description:  计算排名，每天0点更新
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/5/12 18:49
     **/
    @Scheduled(cron = "0 0 0 * * ?")
    public void calculateOiRank(){
        taskExecutor.execute(()->{
            log.info("calculateOiRank，线程名：{}" ,Thread.currentThread().getName());
            rankService.calculateOiRank();
        });
    }

    /**
     * @method: [hasOngoingContest]
     * @param: []
     * @returns: void
     * @description: 检测当前是否有比赛，设置论坛状态 （10分钟检查一次）
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/5/12 18:48
     **/
    @Scheduled(cron = "0 */10 * * * ?")
    public void hasOngoingContest(){
        log.info("hasOngoingContest，线程名：{}" ,Thread.currentThread().getName());
        contestInProgressAspect.setContestInProgress(contestService.hasOngoingContest());
    }

}
